package com.kt.mes.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.kt.mes.domain.ProcessRouteItem;
import com.kt.mes.domain.WorkPlan;
import com.kt.mes.domain.WorkPlanItem;
import com.kt.mes.mapper.ProcessRouteItemMapper;
import com.kt.mes.mapper.WorkPlanItemMapper;
import com.kt.mes.mapper.WorkPlanMapper;
import com.kt.mes.util.SerialNumUtil;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kt.mes.mapper.OrderProductMapper;
import com.kt.mes.domain.OrderProduct;
import com.kt.mes.service.IOrderProductService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产订单Service业务层处理
 * 
 * @author lzc
 * @date 2021-01-18
 */
@Service
public class OrderProductServiceImpl implements IOrderProductService 
{
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private SerialNumUtil serialNumUtil;
    @Autowired
    private WorkPlanMapper workPlanMapper;

    @Autowired
    private WorkPlanItemMapper workPlanItemMapper;
    @Autowired
    private ProcessRouteItemMapper processRouteItemMapper;

    /**
     * 查询生产订单
     * 
     * @param id 生产订单ID
     * @return 生产订单
     */
    @Override
    public OrderProduct selectOrderProductById(Long id)
    {
        return orderProductMapper.selectOrderProductById(id);
    }

    /**
     * 查询生产订单列表
     * 
     * @param orderProduct 生产订单
     * @return 生产订单
     */
    @Override
    public List<OrderProduct> selectOrderProductList(OrderProduct orderProduct)
    {
        return orderProductMapper.selectOrderProductList(orderProduct);
    }

    /**
     * 新增生产订单
     * 
     * @param orderProduct 生产订单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOrderProduct(OrderProduct orderProduct)
    {
        //1.创建订单
        orderProduct.setCreateTime(DateUtils.getNowDate());
        orderProduct.setOpState("N");
        orderProduct.setFinQty(0);
       // orderProduct.setOpId("OP"+DateUtils.dateTime()+serialNumUtil.getNewAutoNum());
        orderProduct.setOpId(DateUtils.dateTime()+serialNumUtil.getNewAutoNum());
        orderProductMapper.insertOrderProduct(orderProduct);

        //2.创建计划

        WorkPlan wp=new WorkPlan();
        wp.setoId(orderProduct.getOpId());
        wp.setFinPos(0);
        wp.setSumPos(orderProduct.getProQty());
        wp.setWpState("N");
        wp.setCreateTime(new Date());
        workPlanMapper.insertWorkPlan(wp);

        //3.创建计划项目

        ProcessRouteItem query_processRouteItem=new ProcessRouteItem();
        query_processRouteItem.setpId(orderProduct.getPpId());
        List<ProcessRouteItem>  list_pri= processRouteItemMapper.selectProcessRouteItemList(query_processRouteItem);

        for(int i=0;i<wp.getSumPos();i++){

            WorkPlanItem wpi=new WorkPlanItem();
            wpi.setOpId(orderProduct.getOpId());
            wpi.setOpsNum(i+1);
            wpi.setwId(wp.getId());
            //根据工艺 生成计划
            if(list_pri.isEmpty()==false){
                for(ProcessRouteItem pr:list_pri){
                        wpi.setPlId(pr.getPlId());//产线
                        wpi.setStepNum(pr.getPriNum()); //步骤
                        wpi.setSiteId(pr.getSiteId());
                        wpi.setActId(pr.getSiteAct());
                        wpi.setWpiState("N");
                        wpi.setTpriId(pr.getId());   //对应工艺号

                    workPlanItemMapper.insertWorkPlanItem(wpi);
                }
            }
            
        }



        return  1;


    }

    /**
     * 修改生产订单
     * 
     * @param orderProduct 生产订单
     * @return 结果
     */
    @Override
    public int updateOrderProduct(OrderProduct orderProduct)
    {
        orderProduct.setUpdateTime(DateUtils.getNowDate());
        return orderProductMapper.updateOrderProduct(orderProduct);
    }

    /**
     * 批量删除生产订单
     * 
     * @param ids 需要删除的生产订单ID
     * @return 结果
     */
    @Override
    public int deleteOrderProductByIds(Long[] ids)
    {




        return orderProductMapper.deleteOrderProductByIds(ids);
    }

    /**
     * 删除生产订单信息
     * 
     * @param id 生产订单ID
     * @return 结果
     */
    @Override
    public int deleteOrderProductById(Long id)
    {


        //订单删除 工作计划删除
        OrderProduct op=  selectOrderProductById(id);
        WorkPlan wp=new WorkPlan();
        wp.setoId(op.getOpId());
        List<WorkPlan>  list_wp=  workPlanMapper.selectWorkPlanList(wp);
        if(!list_wp.isEmpty()){

            list_wp.forEach(item->{
                workPlanMapper.deleteWorkPlanById( item.getId());
            });
        }


        return orderProductMapper.deleteOrderProductById(id);
    }

    @Override
    public List<Map<String,Object>> findQtyGroupDay() {
        return orderProductMapper.findQtyGroupDay();
    }
}
